package com.meritdata.cloud.data.service.development.service;

import com.meritdata.cloud.data.service.development.model.DataApiCategory;
import com.meritdata.cloud.data.service.development.repository.IDataApiCategoryDao;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class DataApiCategoryService  {
    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Autowired
    private IDataApiCategoryDao iDataApiCategoryDao;

    public void add(DataApiCategory category) {
        iDataApiCategoryDao.save(category);
    }

    public List<DataApiCategory> all() {
        return iDataApiCategoryDao.findAll();
    }

    public boolean save(DataApiCategory category) {
        Date now = new Date();
        category.setUpdateTime(now);
        iDataApiCategoryDao.save(category);
        return true;
    }

    public Optional<DataApiCategory> get(String id) {
        return iDataApiCategoryDao.findById(Long.valueOf(id));
    }

    public boolean delete(String id) {
        iDataApiCategoryDao.deleteById(Long.valueOf(id));
        return true;
    }
    @Modifying
    public boolean udpate(String id,String name,Date updateTime) {
        return iDataApiCategoryDao.update(Long.valueOf(id),name,updateTime) >=0;
    }

    public List<DataApiCategory> tree() {
        List<DataApiCategory> categorys =  all();
        return categorys;
    }

}
